package gspushmq.v1.subscribe.handler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.DBCursor;
import ggframework.mongodb.DBObject;
import gspushmq.util.DateUtil;
import gspushmq.util.MongoUtil;
public class GGProduceUtil {

	public static final String API = "api";
	public static final String COUNT = "count";
	public static final String LIST = "list";
	
	
	
	/**
	 * 
	 * 我的自选股-获取自选股分组
	 * 
	 * @param businessCode
	 * @param condition
	 * @return
	 * 
	 * @author shiyb
	 * @date 2014年5月13日 下午1:33:41
	 */
	public static Long getMyStockGroupList(Long accountId) {
		DBCollection collection = MongoUtil.getFtAccountDataCollection("cas_mystockgroup");
		BasicDBObject match = new BasicDBObject();
		match.append("status", 1).append("group_class", 1).append("accountid", accountId);
		DBCursor cursor = collection.find(match);
		Long id = null;
		while(cursor.hasNext()){
			DBObject o = cursor.next();
			id = o.getLong("_id");
		}
		cursor.close();
		return id;
	}
	
	/**
	 * 根据accountid获取自选股代码 用于新接口
	 * 
	 * @param accountId 用户ID
	 * @param stockClass 自选股类型（2：重点；1：普通；0：无）
	 * @param groupId 自选股分组ID
	 * @param groupClass 自选股分组分类
	 * @param type 1为资讯新闻、2为公告
	 * @return
	 * 
	 * @author shaohui
	 * @date 2014年8月5日 下午7:48:48
	 */
	public static List<String> getNewMyStockCodesList(Long accountId, Integer stockClass, Long groupId, Integer groupClass) {
		if(accountId == null){
			return null;
		}
		List<String> stockCodes = new ArrayList<String>();
		
		DBCollection collection = MongoUtil.getFtAccountDataCollection("cas_mystock");
		BasicDBObject query = new BasicDBObject("accountid", accountId).append("status", 1).append("type", new BasicDBObject("$in", Arrays.asList(null, 1)));
		if (stockClass != null) {
			query.append("stockclass", stockClass);
		}
		if (groupId != null) {
			if(groupId == -1){
				query.put("status", 0);
			}else{
				query.append("groupid", groupId);
			}
		}
		if (groupClass != null) {
			query.append("groupclass", groupClass);
		}
		BasicDBObject myStockShowColumn = new BasicDBObject("_id", 0).append("stockcode", 1)
											.append("status",1).append("delete_right_coefficient", 1);
		DBCursor cursor = collection.find(query, myStockShowColumn);
		while(cursor.hasNext()) {
			DBObject dbObject = cursor.next();
			
			if (dbObject.get("stockcode") != null) {
				String stockCode =null;
				if(groupId != null && groupId == -1 ){ //处理已删除的组合中代码
					if(dbObject.get("delete_right_coefficient") != null){
						if(stockCodes.contains(dbObject.get("stockcode").toString())){
							continue;
						}
					}
				}
				stockCode= dbObject.get("stockcode").toString();
				stockCodes.add(stockCode);
			}
		}
		cursor.close();
		return stockCodes;
	}
	
	public enum StockScore{
		TOP300;
		
		private Date preDate;
		private List<String> top300StockCodes;
		
		public List<String> getTop300(){
			Date currentDate = DateUtil.getBeginOfDay();
			if(DateUtil.compare(currentDate, preDate) > 0){
				reload();
			}
			
			return top300StockCodes;
		}
		
		/**
		 * 由于使用的是单例模式，以下代码是为了每天都重新获取一次成分股
		 * 
		 * 
		 * @author yutao
		 * @date 2017年4月10日下午2:44:33
		 */
		private void reload(){
			top300StockCodes = getTop300StockCodes();
			this.preDate = DateUtil.getBeginOfDay();
		}
	}
	
	/**
	 * 获取top300的股票代码
	 * 
	 * @author yutao
	 * @return 
	 * @date 2018年4月2日下午1:13:03
	 */
	private static List<String> getTop300StockCodes(){
		DBCollection score = MongoUtil.getGGStockCollection("t_api_stock_score_new");
		BasicDBObject sort = new BasicDBObject();
		sort.append("con_date", -1).append("total_score", -1);
		BasicDBObject fields = new BasicDBObject();
		fields.append("con_date", 1);
		DBCursor sc = score.find(new BasicDBObject(), fields).sort(sort).limit(1);
		Date cDate = null;
		while(sc.hasNext()){
			DBObject o = sc.next();
			cDate = o.getDate("con_date");
		}
		sc.close();
		
		BasicDBObject query = new BasicDBObject();
		BasicDBObject timeQuery = new BasicDBObject();
		timeQuery.append("$gte", cDate).append("$lte", cDate);
		query.append("con_date", timeQuery);
		DBCursor cursor = score.find(query).sort(sort).limit(300);
		List<String> codeList = new ArrayList<String>();
		while(cursor.hasNext()){
			DBObject o = cursor.next();
			codeList.add(o.getString("stock_code"));
		}
		cursor.close();
		return codeList;
	}
}
